package com.ljf.utility.domain.service;

import com.ljf.common.domain.service.NoDBBasisDomainService;
import com.ljf.common.exception.ServiceException;
import com.ljf.common.utils.MathUtil;
import com.ljf.common.utils.StringUtil;
import com.ljf.utility.dto.other.CalculateMath;
import com.ljf.utility.vo.CalculateVO;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 计算器  领域服务
 *
 * @author LJF
 * @since 2024-08-11
 */
@Component
public class CalculateDomainService extends NoDBBasisDomainService {

    /**
     * 数学计算器接口
     *
     * @param model
     * @return
     * @throws ServiceException
     */
    public List<CalculateVO> mathCalculator(CalculateMath model) throws ServiceException {
        List<String> dataList = new ArrayList<>();
        for (String equation : model.getEquationList()) {
            String prefix = StringUtil.extractNonDigitPrefix(equation);
            dataList.add(prefix + MathUtil.calculator(equation.substring(prefix.length()), model.getDecimalPlaces()));
        }
        List<CalculateVO> resultList = new ArrayList<>();
        resultList.add(CalculateVO.builder().dataList(dataList).build());
        return resultList;
    }

}
